import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:thunder/l10n/generated/app_localizations.dart';

import 'package:thunder/src/features/post/post.dart';
import 'package:thunder/src/app/bloc/thunder_bloc.dart';
import 'package:thunder/src/app/utils/global_context.dart';

enum FeedFabAction {
  openFab(),
  backToTop(),
  subscriptions(),
  changeSort(),
  refresh(),
  dismissRead(),
  newPost();

  IconData get icon {
    switch (this) {
      case FeedFabAction.openFab:
        return Icons.more_horiz_rounded;
      case FeedFabAction.backToTop:
        return Icons.arrow_upward;
      case FeedFabAction.subscriptions:
        return Icons.people_rounded;
      case FeedFabAction.changeSort:
        return Icons.sort_rounded;
      case FeedFabAction.refresh:
        return Icons.refresh_rounded;
      case FeedFabAction.dismissRead:
        return Icons.clear_all_rounded;
      case FeedFabAction.newPost:
        return Icons.add_rounded;
    }
  }

  String get title {
    switch (this) {
      case FeedFabAction.openFab:
        return AppLocalizations.of(GlobalContext.context)!.open;
      case FeedFabAction.backToTop:
        return AppLocalizations.of(GlobalContext.context)!.backToTop;
      case FeedFabAction.subscriptions:
        return AppLocalizations.of(GlobalContext.context)!.subscriptions;
      case FeedFabAction.changeSort:
        return AppLocalizations.of(GlobalContext.context)!.changeSort;
      case FeedFabAction.refresh:
        return AppLocalizations.of(GlobalContext.context)!.refresh;
      case FeedFabAction.dismissRead:
        return AppLocalizations.of(GlobalContext.context)!.dismissRead;
      case FeedFabAction.newPost:
        return AppLocalizations.of(GlobalContext.context)!.createPost;
    }
  }
}

enum PostFabAction {
  openFab(),
  backToTop(),
  changeSort(),
  replyToPost(),
  refresh(),
  search();

  IconData getIcon({IconData? override, bool postLocked = false}) {
    if (override != null) {
      return override;
    }

    switch (this) {
      case PostFabAction.openFab:
        return Icons.more_horiz_rounded;
      case PostFabAction.backToTop:
        return Icons.arrow_upward;
      case PostFabAction.changeSort:
        return Icons.sort_rounded;
      case PostFabAction.replyToPost:
        if (postLocked) {
          return Icons.lock;
        }
        return Icons.reply_rounded;
      case PostFabAction.refresh:
        return Icons.refresh_rounded;
      case PostFabAction.search:
        return Icons.search_rounded;
    }
  }

  String getTitle(BuildContext context, {bool postLocked = false}) {
    switch (this) {
      case PostFabAction.openFab:
        return AppLocalizations.of(context)!.open;
      case PostFabAction.backToTop:
        return AppLocalizations.of(context)!.backToTop;
      case PostFabAction.changeSort:
        return AppLocalizations.of(context)!.changeSort;
      case PostFabAction.replyToPost:
        if (postLocked) {
          return AppLocalizations.of(context)!.postLocked;
        }
        return AppLocalizations.of(context)!.replyToPost;
      case PostFabAction.refresh:
        return AppLocalizations.of(context)!.refresh;
      case PostFabAction.search:
        return AppLocalizations.of(context)!.search;
    }
  }

  void execute({BuildContext? context, void Function()? override, ThunderPost? post, int? postId, int? highlightedCommentId, String? selectedCommentPath}) {
    if (override != null) {
      override();
      return;
    }

    switch (this) {
      case PostFabAction.openFab:
        context?.read<ThunderBloc>().add(const OnFabToggle(true));
      case PostFabAction.refresh:
        context?.read<PostBloc>().add(GetPostEvent(post: post, postId: postId, highlightedCommentId: highlightedCommentId, selectedCommentPath: selectedCommentPath));
      default:
        break;
    }
  }
}
